<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新闻管理系统</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/AdminLTE.css">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/_all-skins.min.css">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/infoStyle.css">
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.5.1.min.js"></script>
</head>
<script>
	$(function () {

		requestData(1);
		$('#page').bind('keypress',function(event){
			if(event.keyCode == "13")
			{
				var page=$("#page").val();
				if(page>=1&&page<=pages)
				{
					requestData(page);
				}
			}
		});

		$("#textId").keyup(function () {
			var content=$(this).val();
			if(content==""){
				return;
			}

			searchDate(pageNum,content);
		});


	});

	function requestData(page) {

		$.post("${pageContext.request.contextPath}/news/ajaxNewsInfo",{index:page},function (data) {
			//当前页
			pageNum = data.pageNum;

			// //每页的数量
			var pageSize=data.pageSize;

			// //当前页的数量
			var size=data.size;


			// //总记录数
			var total = data.total;
			$('#total').text(total);
			// //总页数
			pages = data.pages;
			$('#pages').text(pages);
			// //结果集 size就是list的长度
			var list=data.list;



			showTable(list,size);
		},"json")
	}
	function deleteData(list){
		$.post("${pageContext.request.contextPath}/news/deleteNews",{"list[]":list},function (data) {
			//当前页
			var msg = data.msg;
			if(msg=="删除成功！"){
				alert("删除成功！");
				requestData(1);
			}else{
				alert("删除失败！");
			}
		},"json")
	}
	function showTable(list,size) {
		$('#tb tr:gt(0)').remove();
		for(var i=0;i<size;i++){
			var s;
			var s2='<input type="checkbox" name="vid" value="'+list[i].id+'"/>'
			var button=' <button type="button" class="btn bg-olive btn-xs" onclick="detailsBtn( '+list[i].id+')">详情</button>\n' +
					'<button type="button" class="btn bg-olive btn-xs" onclick="modifyBtn( '+list[i].id+')">编辑</button>\n'
			s='<tr><td>'+s2+'</td><td>'+list[i].id+'</td><td>'+list[i].title+'</td><td>'+list[i].writer+'</td><td>'+list[i].releaseDate+'</td><td>'+button+'</td></tr>';
			$("#tb").append(s);
		}
		$("#page").val(pageNum);
	}
	function pageclick1() {
		requestData(1);
	}

	function pageclick2() {
		if(pageNum!=1){
			requestData(pageNum-1);
		}
	}
	function pageclick3() {
		if(pageNum!=pages){
			requestData(pageNum+1);
		}
	}
	function pageclick4() {
		requestData(pages);
	}
	function search(){
		var string =$("#textId").val();
		if(!(string==null||string=="")){
			searchDate(pageNum,string);
		}

	}
	//删除按钮
	function deleteBtn() {
		var r=confirm("是否确认删除？")
		if(r==true) {
			var list = [];
			$("input:checkbox[name='vid']:checked").each(function () {
				list.push($(this).val());
			});
			deleteData(list);
		}
	}
	//刷新按钮
	function refreshBtn() {
		$('#textId').val("");
		requestData(1);
	}
	//编辑按钮
	function modifyBtn(id) {
		var url='${pageContext.request.contextPath}/news/modifyNews?id='+id;
		window.open (url);
	}
	//详情按钮
	function detailsBtn(id) {
		var url='${pageContext.request.contextPath}/news/news?id='+id;
		window.open (url);
	}
	//搜索按钮
	function searchDate(page,string) {

		$.post("${pageContext.request.contextPath}/news/findByKeyword",{key:string,index:page},function (data) {
			//当前页
			pageNum = data.pageNum;

			// //每页的数量
			var pageSize=data.pageSize;

			// //当前页的数量
			var size=data.size;

			// //总记录数
			var total = data.total;
			$('#total').text(total);
			// //总页数
			pages = data.pages;
			$('#pages').text(pages);
			// //结果集 size就是list的长度
			var list=data.list;

			$("#textId").val(string);
			showTable(list,size);
		},"json")
	}
	function exitBtn() {
		var r=confirm("确认退出？")
		if(r==true){
			window.location.href="${pageContext.request.contextPath}/admin/shutdown";
		}
	}
</script>
<style>
	.button{
		border-right: blue 1px solid;
		border-top: blue 1px solid;
		width: 50px;
		height: 50px;
		float:right;
		background:url('${pageContext.request.contextPath}/images/exit.png') no-repeat;
		border-style: none;
		background-size: cover;
	}
	.searchBtn{
		width: 50px;
		height: 40px;
		background:url('${pageContext.request.contextPath}/images/search.png') no-repeat;
		border-style: none;
		background-size: cover;
	}

</style>
<body class="hold-transition skin-blue sidebar-mini">
    <div class="wrapper">
        <!-- 页面头部 -->
       <!-- 页面头部 -->
               <header class="main-header">
       
                   <!-- Header Navbar: style can be found in header.less -->
                   <nav class="navbar navbar-static-top"style="text-align: center;">
                       <!-- Sidebar toggle button-->
					   <button type="button" class="button" onclick="exitBtn()"></button>
                   </nav>
               </header>
               <!-- 页面头部 /-->
        <!-- 导航侧栏 -->
        <aside class="main-sidebar">
         
            <section class="sidebar">    
                <ul class="sidebar-menu">
                	<li class="header">菜单</li>
                    <!-- 菜单 -->
					<li id="admin-index1"><a href="${pageContext.request.contextPath}/admin/index"><i class=""></i> <span>首页</span></a></li>
					<li id="admin-index2"><a href="${pageContext.request.contextPath}/admin/showNewsInfo"><i class=""></i> <span>新闻列表</span></a></li>
					<li id="admin-index3"><a href="${pageContext.request.contextPath}/news/addNews"><i class=""></i> <span>发布新闻</span></a></li>
					<li id="admin-index4"><a href="${pageContext.request.contextPath}/admin/showDraftsInfo"><i class=""></i>
						<span>草稿箱</span></a></li>
                    <!-- 菜单 /-->
                </ul>
            </section>
        </aside>
        <!-- 导航侧栏 /-->

        <!-- 内容区域 -->
        <div class="content-wrapper"  >
            <section class="content" >
               <!---输入内容---->
			   	<div style="text-align:center;">

			   			<label style="font-size:17px">新闻搜索：</label>
			   			<input type="text" style="width:500px;font-size:17px" id="textId"/>
						<button class="btn btn-default" style="width:70px;background:#39cc;margin-top:-5px;font-weight:bold" onclick="search()">搜索</button>
						<button class="btn btn-default" style="width:70px;background:#39cc;float:right;margin-top:-3px;font-weight:bold" onclick="deleteBtn()">删除</button>
                         <button class="btn btn-default" style="width:70px;background:#39cc;float:left;margin-top:-3px;font-weight:bold" id="reBtn" value="refresh" onclick="refreshBtn()">刷新</button>


                </div>
			   	<table id="tb" class="table">
			   		<tr>
						<th style="width:30px"></th>
			   			<th style="width:130px">新闻编号</th>
			   			<th style="width:200px">新闻标题</th>
			   			<th style="width:60px">作者</th>
			   			<th style="width:130px">发布时间</th>
			   			<th style="width:120px"></th>
			   		</tr>
			   		<tr>
			   			<td><input type="checkbox" name="id" value="id"/></td>
			   			<td>02115611354</td>
			   			<td>我校召开期中教学检查工作总结视频会</td>
			   			<td>刘金昆</td>
			   			<td>2020-05-15</td>
			   			<td>否</td>
			   			<td style="width:100px">
			   				 <button type="button" class="btn bg-olive btn-xs">详情</button>
			   				 <button type="button" class="btn bg-olive btn-xs">编辑</button>
                             <button type="button" class="btn bg-olive btn-xs">删除</button>
							< a href="modify_news.jsp"  target="_blank" >页面</a>
			   			</td>
			   		</tr>





			   	</table>
                <div style="float: left">
                    总计 <label id="total" style="font-size: 16px"></label>&nbsp;&nbsp;条&nbsp;&nbsp;共
                    <label id="pages" style="font-size: 16px"></label> 页
                </div>
                <div  style="float:right">
                    	 <button type="button" class="btn btn-default" onclick="pageclick1()">首页</button>
                    	<button type="button" class="btn btn-default" onclick="pageclick2()">上一页</button>
                    	page <input type="text" style="width:50px" id="page"/>
                    	<button type="button" class="btn btn-default" onclick="pageclick3()">下一页</button>
                    	<button type="button" class="btn btn-default" onclick="pageclick4()">尾页</button>
			   	</div>
			   	
                <!-- /.error-page -->
            </section>
            <!-- /.content -->
        </div>
        <!-- 内容区域 /-->
        <!-- 底部导航 -->
        <footer class="main-footer">
            <strong>Copyright &copy; web第三小组</strong>
        </footer>
        <!-- 底部导航 /-->
    </div>
</body>
</html>